package com.jk.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jk.dao.EsDao;
import com.jk.dao.ProductDao;
import com.jk.entity.Product;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class EsService {
    @Autowired
    private EsDao esDao;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private RedisTemplate redisTemplate;

    public void tong() {
        List<Product> listPros = productDao.getListPros();
        for (int i = 0; i <listPros.size() ; i++) {
            esDao.save(listPros.get(i));
        }
    }

    public List<Map<String,Object>> getListPros(String name) {

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if(!StringUtils.isEmpty(name))
            boolQueryBuilder.must(QueryBuilders.matchQuery("product_name",name));

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("product_name");
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");


        SearchRequestBuilder movieAction = elasticsearchTemplate.getClient().prepareSearch("product");

        movieAction.setQuery(boolQueryBuilder)
                .highlighter(highlightBuilder)
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) //设置查询类
                .addSort("product_id", SortOrder.DESC);


            SearchResponse searchResponse = movieAction.get();
            SearchHits hits = searchResponse.getHits();
            SearchHit[] hitsHits = hits.getHits();//总数居
            Integer total = (int)hits.getTotalHits();
            List<Map<String,Object>> list = new ArrayList();
            for (int i = 0; i < hitsHits.length; i++) {
                Map<String, Object> sourceAsMap = hitsHits[i].getSourceAsMap();
                Map<String, HighlightField> highlightFields = hitsHits[i].getHighlightFields();
                if (!StringUtils.isEmpty(name)){
                    sourceAsMap.put("product_name",highlightFields.get("product_name").getFragments()[0].toString());
                }
                list.add(sourceAsMap);
            }
        Gson gson = new Gson();
        //定义key
        String   cacheKey = "Product";

        redisTemplate.opsForValue().set(cacheKey,gson.toJson(list),'1', TimeUnit.SECONDS);
            return list;
        }

}
